//
//  BaseHttpDataManger.m
//  Timingbar
//
//  Created by zibin on 2017/7/19.
//  Copyright © 2017年 timingbar. All rights reserved.
//

#import "BaseHttpDataManger.h"

@implementation BaseHttpDataManger
- (NSURLSessionDataTask *)sendGetWithModel:(BaseResquestModel *)requestModel
                             ResponseClass:(Class)responseClass
                            sucessResponse:(sucessResponseBlock)sucessBlock
                              failResponse:(failResponseBlock)failBlock
{
    NetWorkResquestTool *requestTool = [[NetWorkResquestTool alloc]init];
    WS(weakSelf);
    return [requestTool httpGetWithModel:requestModel ResponseClass:responseClass sucessResponse:^(id response) {
        [weakSelf callback:response error:nil sucess:sucessBlock fail:failBlock];
    } failResponse:^(NSError *error) {
        [weakSelf callback:nil error:error sucess:sucessBlock fail:failBlock];
    }];
}

- (NSURLSessionDataTask *)sendPUTWithModel:(BaseResquestModel *)requestModel
                             ResponseClass:(Class)responseClass
                            sucessResponse:(sucessResponseBlock)sucessBlock
                              failResponse:(failResponseBlock)failBlock
{
    NetWorkResquestTool *requestTool = [[NetWorkResquestTool alloc]init];
    WS(weakSelf);
    return [requestTool httpPUTWithModel:requestModel ResponseClass:responseClass sucessResponse:^(id response) {
        [weakSelf callback:response error:nil sucess:sucessBlock fail:failBlock];
    } failResponse:^(NSError *error) {
        [weakSelf callback:nil error:error sucess:sucessBlock fail:failBlock];
    }];
}


- (NSURLSessionDataTask *)sendPostWithModel:(BaseResquestModel *)requestModel
                              ResponseClass:(Class)responseClass
                             sucessResponse:(sucessResponseBlock)sucessBlock
                               failResponse:(failResponseBlock)failBlock
{
    NetWorkResquestTool *requestTool = [[NetWorkResquestTool alloc]init];
    WS(weakSelf);
    return [requestTool httpPostWithModel:requestModel ResponseClass:responseClass sucessResponse:^(id response) {
        [weakSelf callback:response error:nil sucess:sucessBlock fail:failBlock];
    } failResponse:^(NSError *error) {
        [weakSelf callback:nil error:error sucess:sucessBlock fail:failBlock];
    }];
}

//回调处理
- (void)callback:(id)data error:(NSError *)error sucess:(void(^)(id))sucess fail:(void(^)(NSError *error))fail
{
    if(error && fail)
    {
        fail(error);
    }
    else
    {
        if(sucess)
            sucess(data);
    }
}
@end
